<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue.directive</title>
    <script type="text/javascript" src="../assets/js/vue-2.3.0.js"></script>
</head>
<body>
    <h1>vue.directive自定义指令</h1>
    <hr>
    <div id="app">
        <input type="text" v-focus>
        <div v-focus2="color">{{num}}</div>
        <div v-color="color">{{num}}</div>
        <button @click="add">点我</button>
    </div>
    <p>
        <button onclick="unbind()">解绑</button>
    </p>
</body>
<script type="text/javascript">
function unbind(){
    app.$destroy()
}
//注册全局
    Vue.directive('focus',{
        inserted:function(el){
            el.focus()
        }
    })

//钩子函数
    Vue.directive('focus2', {
        bind:function(){
            console.log('1-bind');
        },
        inserted:function(){
            console.log('2-inserted');
        },
        update:function(el,binding){
            el.style.color = binding.value;                    
            console.log('3-update');
        },
        componentUpdated:function(){
            console.log('4-componentUpdated');
        },
        unbind:function(){
            console.log('5-unbind');
        }
    })
    //函数简写
    Vue.directive('color',function(el,binding){
        el.style.color = binding.value;
        // el.style='color:'+binding.value;//写法二
    })

    var app = new Vue({
        el:'#app',
        data:{
            message: 'helloworld!',
            num:10,
            color:'orange'
        },
        methods:{
            add:function(){
                this.num++;
            }
        },
        directives:{   //注册局部
            focus3:{
                inserted:function(el){
                    el.focus()
                }
            }
        }
    })
    
</script>
</html>